﻿#include <iostream>
#include <queue>

using namespace std;

class TreeNode {
public:
    int val;
    TreeNode* left, * right;
    TreeNode(int val) {
        this->val = val;
        this->left = this->right = nullptr;
    }
};


class Solution {
public:
    /**
     * This method will be invoked first, you should design your own algorithm
     * to serialize a binary tree which denote by a root node to a string which
     * can be easily deserialized by your own "deserialize" method later.
     */
    string serialize(TreeNode* root) {
        // write your code here

    }

    /**
     * This method will be invoked second, the argument data is what exactly
     * you serialized at method "serialize", that means the data is not given by
     * system, it's given by your own serialize method. So the format of data is
     * designed by yourself, and deserialize it here as you serialize it in
     * "serialize" method.
     */
    TreeNode* deserialize(string& data) {
        // write your code here
      

        size_t i = 0;
        int v;
        int ret;
        ret = read(data, i, v);
        TreeNode* root = new TreeNode(v);
        TreeNode* node = root;
        TreeNode* rnode,*lnode = nullptr;
        int state = 1;
        while (ret != 3);
        {
            ret = read(data, i, v);
            if (ret == 1)
            {
                TreeNode* newnode = new TreeNode(v);
                node->left = newnode;
            }
  
                if (state == 2)
                {
                    node->left = newnode;
                    node = no;
                    state = 2;
                }
           }
        } 

        return nullptr;
    }

    int read(string& data,size_t& i,int& val)
    {
        val = 0;
        queue<char> a;
        if (i >= data.size()) return 3;
        while (true)
        {
            char c = data[i++];
            if (c >= '0' and c <= '9')
            {
                a.push(c);
            }
            if (c == '#')
            {
                while (data[i] == ',')i++;
                return 2;
            }
            if(c == ',' || i == data.size())
            {
                while (!a.empty())
                {
                    char k = a.front();
                    int g = (int)k - '0';
                    a.pop();
                    int m = a.size();
                    val +=g* (int)pow(10, m);                    
               }
                return 1;
            }
        }
    }
};

int main()
{
    Solution * s = new Solution;
    string a = "3,9,20,#,#,15,7";
    s->deserialize(a);
}



